<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;

/**
 * 首页接口
 */
class Refer extends Api
{
    protected $noNeedLogin = [''];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {
        $this->success('请求成功');
    }
    public function add(){
        $user = $this->auth->getUser();
        $content = $this->request->post('content');
        $type = $this->request->post('type');
        if(!$type || !$content){
            $this->error("缺少比传参数");
        }
        $data = Db::name("material")->where(['uid'=>$user->id])->find();
        if($data & 1 == 2){//修改
            $in_data['content'] = $content;
            $in_data['type'] = $type;
            $in_data['updatetime'] = time();
            $in_status = Db::name("refer")->where(['uid'=>$user->id])->update($in_data);
            if($in_status !== false){
                $this->success("成功");
            }else{
                $this->error("失败");
            }
        }else{//添加
            $in_data['uid'] = $user->id;
            $in_data['content'] = $content;
            $in_data['type'] = $type;
            $in_data['status'] = 'one';
            $in_data['createtime'] = time();
            $in_data['updatetime'] = time();
            $in_status = Db::name("refer")->insertGetId($in_data);
            if($in_status){
                $data = Db::name("refer")->where(['id'=>$in_status])->find();

                    if($data['type'] == 'two'){
                        $data['content'] = cdnurl($data['content'], true);

                    }
                    $avatar = Db::name("user")->where(['id'=>$user->id])->value("avatar");
                $data['avatar'] = cdnurl($avatar, true);

                $this->success("成功",$data);
            }else{
                $this->error("失败");
            }
        }
    }
    public function details(){
        $user = $this->auth->getUser();
        $data = Db::name("refer")->where(['uid'=>$user->id])->select();
        foreach ($data as $k=>$v){
            if($v['type'] == 'two'){
                $data[$k]['content'] = cdnurl($v['content'], true);

            }
            if($v['type'] == 'three'){
                /*$arr = explode(".",$v['content']);
                $str = '';
                foreach ($arr as $kk=>$vv){
                    $str .= cdnurl($vv, true).',';
                }
                $data[$k]['content'] = trim($str,',');*/
                $data[$k]['content'] = cdnurl($v['content'], true);
            }
            if($v['status'] == 'one'){
                $material = Db::name("material")->where(['id'=>$user->id])->find();
                $data[$k]['sex'] = $material['sex'];

            }else{
                $data[$k]['sex'] = '';
            }
            $avatar = Db::name("user")->where(['id'=>$user->id])->value("avatar");

            $data[$k]['avatar'] = cdnurl($avatar, true);

        }
        $this->success("成功",$data);
    }
    public function userRecovery(){
        $user = $this->auth->getUser();
        $data = Db::name("recovery")->field("createtime,product_ids")->where(['uid'=>$user->id])->order("id desc")->select();
        foreach ($data as $k=>$v){
            $data[$k]['createtime'] = date('Y-m-d',$v['createtime']);
            $data[$k]['product_list'] = Db::name("product")->field("name")->where('id','in',$v['product_ids'])->select();;
        }
        $this->success("成功",$data);
    }
}
